#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include "bootutil.h"
#include "AtaDevice.h"

IDEDriveInformation info;
char buf[41];
char sector[512];

char num[] = {'0', '1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F'};
void print_hex(int dat)
{
	int c1, c2;
	c1 = dat>>4 & 0xF;
	c2 = dat & 0xF;

	kprintf("%c%c", num[c1], num[c2]);
}

char * sbrk(int incr)
{
	unsigned long prev_heap_end;
	__asm__ __volatile__(
		"mov %1, %%ebx\t\n"
		"int $0x90\t\n"
		"mov %%eax, %0"
		:"=m"(prev_heap_end):"m"(incr),"a"(0x1001)
	);
	return (char *)prev_heap_end;
}

int main(void)
{
printf("Hello");
	init_video();

	AtaDevice ide(IDE_PRIMARY_CONTROLLER, IDE_DRIVE0);
	char * buf = new char[64];
	ide.GetModelName(buf, 64);
	kprintf("Model: %s\n", buf);
	ide.GetSerialNumber(buf, 64);
	kprintf("SN: %s\n", buf);
	ide.GetFirmwareRevision(buf, 64);
	kprintf("Ver: %s\n", buf);

	free(buf);
	buf = new char[512];
	ide.ReadSector(0, 1, (unsigned char *)buf);
	
	// Output sector information
	for(int i=0; i<4; i++)
	{
		kprintf(" %d  ", i);
		for(int j=0; j<8; j++)
		{
			print_hex(buf[i*16+j]);
			kprintf(" ");
		}
		kprintf("    ");
		for(int j=8; j<16; j++)
		{
			print_hex(buf[i*16+j]);
			kprintf(" ");
		}
		kprintf("\n");
	} 	
	kprintf("...\n");
	for(int i=24; i<32; i++)
	{
		kprintf("%d  ", i);
		for(int j=0; j<8; j++)
		{
			print_hex(buf[i*16+j]);
			kprintf(" ");
		}

		kprintf("    ");
		for(int j=8; j<16; j++)
		{
			print_hex(buf[i*16+j]);
			kprintf(" ");
		}
		kprintf("\n");
	} 	

	kprintf("\n");

	for(int i=0; i<4; i++)
	{
		PartitionInfo pi = ide.GetPartitionInfo(i);
		kprintf("0x%x   0x%x   0x%x   0x%x\n", pi.active, pi.type, pi.start_lba, pi.size);
	}

	return 0;
}
